package com.learn;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Scanner;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class PCB {
    List<Paragraph> paragraphList;
    //分页长度
    int pageLength;
    
    //用以区别是段式还是段页式,true为段式false为段页式
    boolean flag;

    public void operation(){
        Scanner scanner = new Scanner(System.in);
        if(flag){
            System.out.println("该进程控制块采用段式管理");
            int size = paragraphList.size();
            System.out.println("该进程被分为"+size+"段,段号为0-"+(size-1)+"请输入段号");
            int paragraphNo = scanner.nextInt();
            if(paragraphNo<0||paragraphNo>=size){
                System.out.println("段号越界！");
                return;
            }
            Paragraph paragraph = paragraphList.get(paragraphNo);
            int paragraphLength = paragraph.getParagraphLength();
            System.out.println("该段段长为"+paragraphLength+"段基址为"+paragraph.getParagraphStart()+"请输入段内偏移地址");
            int offset = scanner.nextInt();
            if(offset<0||offset>=paragraphLength){
                System.out.println("偏移地址越界！");
                return;
            }
            int result;
            result=offset+paragraph.getParagraphStart();
            System.out.println("物理地址为"+result);
            return;
        }else {
            System.out.println("该进程控制块采用段页式管理");
            int size = paragraphList.size();
            System.out.println("每页长度为"+pageLength+"该进程被分为"+size+"段,段号为0-"+(size-1)+"请输入段号");
            int paragraphNo = scanner.nextInt();
            if(paragraphNo<0||paragraphNo>=size){
                System.out.println("段号越界！");
                return;
            }
            Paragraph paragraph = paragraphList.get(paragraphNo);
            List<Page> pages = paragraph.getPages();
            System.out.println("页表编号对应的内存页表编号");
            for(int i=0;i<pages.size();i++){
                System.out.println(i+"\t"+pages.get(i).getPageNo());
            }
            System.out.println("请输入页号");
            int pageNo = scanner.nextInt();
            if(pageNo<0||pageNo>=pages.size()){
                System.out.println("页号溢出");
                return;
            }
            System.out.println("请输入偏移量");
            int offset = scanner.nextInt();
            if(offset>=pageLength||offset<0){
                System.out.println("偏移量溢出");
                return;
            }
            int result;
            result=pages.get(pageNo).pageNo*pageLength+offset;
            System.out.println("物理地址为"+result);
            return;
        }
    }
}
